/*******************************************************************************
 * Copyright (c) 2013 Zero Square LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Zero Square LLC. Version 1
 * which accompanies this distribution
 ******************************************************************************/
package com.zsqr.dashboard.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.util.Assert;

import com.zsqr.dashboard.commons.dao.GenericJpaDao;
import com.zsqr.dashboard.domain.UserCardEntity;
import com.zsqr.dashboard.domain.UserEntity;

import org.hibernate.transform.Transformers;

public class UserCardJpaDao extends GenericJpaDao<UserCardEntity,Long>implements UserCardDao {

	public UserCardJpaDao() {
		super(UserCardEntity.class);
		}

	
	@SuppressWarnings("unchecked")
	public List<UserCardEntity> loadCardsByUserName(String userName) {
		//null check for username
		Assert.notNull(userName);
		
		List<UserCardEntity> userCard = new ArrayList<UserCardEntity>();		
		List<UserCardEntity> userCardsList = new ArrayList<UserCardEntity>();
		//run query to get whole entity and load object in memory
		//TypedQuery<UserCardEntity>
		TypedQuery<UserCardEntity> query = getEntityManager().createQuery("select  u from " + getPersistenceClass().getSimpleName()
				+ " u where u.userName=:userName",UserCardEntity.class).setParameter("userName", userName);
		Query query1 = getEntityManager().createQuery("select  u from " + getPersistenceClass().getSimpleName()
				+ " u where u.userName=:userName").setParameter("userName", userName);
		//List fruitList = getEntityManager().createQuery("select  u from " + getPersistenceClass().getSimpleName()
			//	+ " u where u.userName=:userName",UserEntity.class).setParameter("userName", userName))
				//.setResultTransformer( Transformers.aliasToBean(UserCardEntity.class)).list();
		//List<UserCardEntity> userCardS = new ArrayList<UserCardEntity>();
		//userCardS.add((UserCardEntity) fruitList.get(0));
		//userCardS.add((UserCardEntity) fruitList.get(1));
		
		
		List<UserCardEntity> userCardS = new ArrayList<UserCardEntity>();
		//catch object in UserEntity
		try{			
			getEntityManager().flush();
			List<UserCardEntity> userCards = new ArrayList<UserCardEntity>();
			
			userCards.addAll(0, query.getResultList());
			
			Iterator<UserCardEntity> results = query.getResultList().iterator();
			
			
			List list = query1.getResultList();
			//userCardsList.add((UserCardEntity)list.get(0));
			//userCardsList.add((UserCardEntity)list.get(1));
			userCardsList = (List<UserCardEntity>)query.getResultList();
			while ( results.hasNext() ) {
			    UserCardEntity usercardEntity = (UserCardEntity)results.next();
			    userCard.add(usercardEntity);
			}
			getEntityManager().flush();
			
			userCardS.addAll(0,(List<UserCardEntity>)query1.getResultList());
			
		}catch(NoResultException e){
			//Do nothing as in case of exception user will retrun null
		}		
		
		return userCardsList;
	}
	
	public UserCardEntity getCardByCardNumber(String cardNumber)
	{
		
		Assert.notNull(cardNumber);
		UserCardEntity userCard = null;
		try{
		//run query to get whole entity and load object in memory
		Query query = getEntityManager().createQuery("select u from " + getPersistenceClass().getSimpleName()
				
				+ " u where u.ccnumber=:ccnumber").setParameter("ccnumber", cardNumber);
		
		//catch object in UserEntity
		userCard = (UserCardEntity)query.getSingleResult();
		}catch(NoResultException e){
			//Do nothing as in case of exception user will retrun null
			return null;
		}		
		return userCard;
		
	}
	
	
	
	
}
